package com.neuedu.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.neuedu.projos.Product;
import com.neuedu.service.ProServ;
import com.neuedu.service.ProServImpl;

/**
 * Servlet implementation class addProServ
 */
@WebServlet("/addProServ")
public class addProServ extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addProServ() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DiskFileItemFactory disk = new DiskFileItemFactory();
		ServletFileUpload servletfileupload= new ServletFileUpload(disk);
		servletfileupload.setHeaderEncoding("utf-8");
		servletfileupload.setFileSizeMax(10*1024*1024);
		String imgName =null;
		String proName =null;
		String proPrice =null;
		String proCount=null ;
		String proMan =null;
		String proDate =null;
		String proDes =null;
		String proPlace=null;
		String cId=null;
		try {
			List<FileItem> parseRequest =servletfileupload.parseRequest(request);
			for (FileItem fileItem : parseRequest) {
				if(fileItem.isFormField())
				{
					String name = fileItem.getFieldName();
					if(name.equals("proName"))
					{
						proName =new String(fileItem.getString("utf-8"));
					}
					if(name.equals("proPrice"))
					{
						proPrice =new String(fileItem.getString("utf-8"));
					}
					if(name.equals("proCount"))
					{
						proCount =new String(fileItem.getString("utf-8"));
					}
					if(name.equals("proMan"))
					{
						proMan=new String(fileItem.getString("utf-8"));
					}
					if(name.equals("proDate"))
					{
						proDate =new String(fileItem.getString("utf-8"));
					}
					if(name.equals("proDes"))
					{
						 proDes =new String(fileItem.getString("utf-8"));
					}
					if(name.equals("proPlace"))
					{
						proPlace =new String(fileItem.getString("utf-8"));
					}
					if(name.equals("cId"))
					{
						cId =new String(fileItem.getString("utf-8"));
					}
					
				}
				if(!(fileItem.isFormField()))
				{
					imgName =fileItem.getName();
					if(!(imgName.endsWith(".jpg")||imgName.endsWith(".png")||imgName.endsWith(".jpeg")))
					{
						request.setAttribute("error", "不能上传此文件类型");
						request.getRequestDispatcher("WEB-INF/page/addpro.jsp").forward(request, response);
						return;
					}
					InputStream inputstream =fileItem.getInputStream();
					File file =new File("D:/test/"+fileItem.getName());
					FileOutputStream fos = new FileOutputStream(file);
					int lean = 0;
					byte[] buffer=new byte[1024];
					while((lean=inputstream.read(buffer))!=-1)
					{
						fos.write(buffer, 0, lean);
					}
					fos.close();
					inputstream.close();
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}
		UUID randomUUID=UUID.randomUUID();
		String subuId=randomUUID.toString().substring(24);
		double ppc =Double.valueOf(proPrice);
		int pCount= Integer.parseInt(proCount);
		Date date = Date.valueOf(proDate);
		int Cid =Integer.parseInt(cId);
		Product pro = new Product(null, subuId, proName, ppc, pCount, proMan, date, imgName, proDes, proPlace, Cid);
		ProServ ps = new ProServImpl();
		ps.addOnePro(pro);
		response.sendRedirect("ShowProListServ2?pageNum=1");
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
